<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_saldo_awal extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_ledger_model', 'GL');
		$this->load->model('combo_model', 'Combo');
		
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Saldo Awal";
		viewByTemplate('keu_saldo_awal/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->GL->GetJSON();
	}
	
		
	function create()
	{
		$data['title'] = "Tambah Setup Saldo Awal";
		$data['data'] = NULL;
		$data['data']['tahun'] = date('Y');
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['activity_data']		= json_encode($this->Combo->SWCostCenter());
		$data['data']['edit'] = 1;
		$data['data']['edit2'] = 1;
		$data['mode'] = "create";
		viewByTemplate('keu_saldo_awal/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah Setup Saldo Awal";
		$data['data'] = $this->GL->GetSingleView(get('tahun'), get('kd_kantor'), get('kd_cost_center'),  get('id_coa'));
		
		$data['data']['saldo_awal'] = $data['data']['begin_db']-$data['data']['begin_cr'];
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['activity_data']		= json_encode($this->Combo->SWCostCenter());
		
		
		$data['mode'] = "edit";
		viewByTemplate('keu_saldo_awal/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("tahun") == '') {
			$error->Add("tahun", "Tahun", $tpl_empty);
		}
		
		if (post("kd_kantor") == '') {
			$error->Add("kd_kantor", "Kantor", $tpl_empty);
		}
		
		if (post("kd_cost_center") == '') {
			$error->Add("kd_cost_center", "Jenis Kegiatan", $tpl_empty);
		}
		
		
		if (post("id_coa") == '') {
			$error->Add("id_coa", "Akun", $tpl_empty);
		}
		
		
		return $error;
	}
	
	function dtl_account_data()
	{
		echo $this->GLDetail->GetJSON();
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('tahun', 'kd_kantor', 'kd_cost_center', 'id_coa', 'saldo_awal'));
				
				
				$saldo_awal		= toNumber($data['saldo_awal']);
				$debet = ($saldo_awal>=0 ? $saldo_awal : 0);
				$kredit = ($saldo_awal>=0 ? 0 : $saldo_awal);
				
				unset($data['saldo_awal']);
				
				if (post("mode") == "create")
				{
					$data['begin_db']			= $debet;
					$data['begin_cr']			= $kredit;
					$data['create_date']		= NOW;
					$data['create_user']		= USERID;

					$this->GL->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					$data['begin_db']			= $debet;
					$data['begin_cr']			= $kredit;
					$data['modify_date']		= NOW;
					$data['modify_user']		= USERID;
					
					$this->GL->Update($data, 
										array('tahun'	=> $data['tahun'],
											  'kd_kantor'	=> $data['kd_kantor'],
											  'kd_cost_center'	=> $data['kd_cost_center'],
											  'id_coa'	=> $data['id_coa']
												));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	
	
}
?>